package com.example.sudoku;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SudokuActivity extends Activity implements OnClickListener {

	private static final String TAG = "Sudoku";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// setup all buttons
		Button continueButton = (Button) findViewById(R.id.button_continue);
		continueButton.setOnClickListener(this);

		Button newGameButton = (Button) findViewById(R.id.button_new_game);
		newGameButton.setOnClickListener(this);

		Button aboutButton = (Button) findViewById(R.id.button_about);
		aboutButton.setOnClickListener(this);

		Button exitButton = (Button) findViewById(R.id.button_exit);
		exitButton.setOnClickListener(this);

	}

	/* menu creation */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	/* menu click */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			startActivity(new Intent(this, Prefs.class));
			break;
		}

		return false;
	}

	/* buttons clicked */
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button_about:
			openAbout();
			break;
		case R.id.button_new_game:
			openNewGameDialog();
			break;
		case R.id.button_exit:
			finish();
			break;
		default:
			break;
		}
	}

	/* when new game clicked */
	private void openNewGameDialog() {

		Builder newGameAlert = new AlertDialog.Builder(this);
		newGameAlert.setTitle(R.string.new_game_title);
		newGameAlert.setItems(R.array.Difficulty,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						startGame(which);
					}
				});

		newGameAlert.show();
	}

	private void startGame(int i) {
		Log.d(TAG, "clicked on " + i);
	}

	private void openAbout() {
		Intent aboutIntent = new Intent(this, About.class);
		startActivity(aboutIntent);
	}

}